<?php

namespace app\common\logic;
use addons\adszone\library\Adszone;
use app\admin\controller\Zlog;
use app\common\model\chat\ChatRoom;
use app\common\model\chat\ChatUser;
use app\common\model\User;
use FFMpeg\Coordinate\TimeCode;
use FFMpeg\FFMpeg;
use think\Db;
use think\exception\ValidateException;


/**
 * 统计热门课程
 */
class ChatLogic
{
    protected $org_cate_id = null ;
    protected $chat = null;
    protected $auth = [];
    public function __construct()
    {
        $this->auth = config('chat_auth');
        $this->org_cate_id = ORG_CATE_ID ? ORG_CATE_ID : 0;
    }

    /**
     * 往期直播
     */
    public function getHistory($where = [], $limit = 15) {
        $courseLogic = new CourseLogic();
        $where['is_chat'] = 1;
        $res = Db::name('learn_course')->alias('c')->join('edu_chat_room r', 'c.id = r.edu_learn_course_id', 'left')->where('c.org_cate_id',ORG_CATE_ID)->where('r.type = 3 or r.type is null')->column('c.id');
        if($res){
            $where['id'] = ['in', $res];
        }else{
            $where['id'] = ['<', 0];
        }
        $list = $courseLogic->getList($where, $limit ,'createtime desc', null);//往期直播

        return $list;
    }

    /*
     * 直播预告 获取所有的  还未结束的直播列表
     * */
    public function getChapter($where = [], $user) {
        $where['org_cate_id'] = $this->org_cate_id;
        $where['type'] = ['neq', 3];
        $is_admin = Db::name('admin')->where('user_id', $user['id'])->find();

//        $where['end_time'] = ['>', time()];
        $chat = new ChatRoom();
        $list = $chat->where($where)->field(true)->select();
        $pic = new Adszone();
        $images = $pic->getAdsByMark('chat_course',ORG_CATE_ID);
        $image = isset($images['data'][0]['imageurl'])?$images['data'][0]['imageurl']:'';
        foreach ($list as $k => &$v) {
            $v->append(['talk_info', 'start_time_text']);
            //权限 打开人的权限问题
            if($is_admin){
                $v['user_auth'] = 1; //可以关闭直播 和开启直播室
            }else{
                $v['user_auth'] = 0;
            }
            $v['image'] = Db::name('learn_course')->where('id', $v['edu_learn_course_id'])->value('image');
            if(empty($v['image'])){
                $v['image'] = $image;
            }
            $v['start_time_str'] = $v['start_time'];
            $v['start_time'] = date('Y-m-d H:i:s', $v['start_time']);

//            if ($user['id'] == $v->user_id) {
//                //当前用户为主持人
//                $v['user_auth'] = 1; //可以关闭直播 和开启直播室
//            } else {
//                $v['user_auth'] = 0;
//            }

        }
        return $list;
    }

    /*
     * 详情
     * */
    public function getChatDetail($id, $user) {
        $chatRoom = new ChatRoom();
        $info = $chatRoom->where(['id'=> $id])->find();
        $is_admin = Db::name('admin')->where('user_id', $user['id'])->find();
        if($info){
            $info->append(['talk_info', 'start_end']);
            //权限 打开人的权限问题
            $auth = [];
           if ($user['id'] == $info['talk_info']['user_id']) {
                $info['user_auth'] = $this->auth['talker'];
            } else {
                if($is_admin){
                    $info['user_auth'] = $this->auth['master']; //可以关闭直播 和开启直播室
                }else{
                    $info['user_auth'] =  $this->auth['listener'];
                }
            }
        }
        //直播室剩余开启秒
        return $info;
    }

    /*获取当前用户头像角色  $id为直播室id*/
    public function getAuth($id, $openid) {
        $room = ChatRoom::get($id);
        $thrid = Db::name('third');
        $user_id = $thrid->where(['openid'=> $openid])->column('user_id');
        $users = User::all(function($query) use ($user_id){
            $query->where(['id' => ['in', $user_id]])->limit(2)->field('nickname, avatar, id');
        });
        foreach ($users as $v) {
            $v['headUrl'] = $v['avatar'];
            if ($v['id'] == $room['user_id']) {
                //当前用户为主持人
                $v['user_auth'] = $this->auth['master']; //可以关闭直播 和开启直播室 为主持人
                return $v;
            } else if ($v['id'] == $room['talk_id']) {
                $v['user_auth'] = $this->auth['talker'];
                return $v;
            } else {
                $v['user_auth'] = $this->auth['listener'];
                return $v;
            }
        }

        return [];
    }


    //根据用户uid 和 room 获取用户角色
    public static function getRole($id, $room) {
        if ($room['user_id'] == $id) {
            return config('chat_auth')['master'];
        }
        $info = ChatUser::get($room['talk_id']);
        if (isset($info['user_id']) && $info['user_id'] == $id) {
            return config('chat_auth')['talker'];
        }

        return config('chat_auth')['listener'];
    }

    /*
     * 生成聊天室 语音合成
     * */
    public function videos() {
        $ffmpeg = FFMpeg::create(array(
            'ffmpeg.binaries'  => exec('which ffmpeg'),
            'ffprobe.binaries' => exec('which ffprobe')
        ));
        $video = $ffmpeg->open(APP_PATH . 'public/uploads/1-1.mp4');
        $frame = $video->frame(TimeCode::fromSeconds(2));//提取第几秒的图像
        $frame->save('image.jpg');
    }
}
